//
//  NewDetailsTableViewCell.m
//  项目A --半糖
//
//  Created by romana on 16/5/24.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "NewDetailsTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "NewThirdDetailsViewController.h"





@implementation NewDetailsTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self doSomething];
    }

    return  self;
    

}


-(void)doSomething{

    self.TopGapView = [[UIView alloc] init];
    self.TopGapView.backgroundColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:0.8];
    [self.contentView addSubview:self.TopGapView];
    
    self.bottomGapView = [[UIView alloc] init];
    self.bottomGapView.backgroundColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:0.8];
    [self.contentView addSubview:self.bottomGapView];
    
    self.smallTitleImageV = [[UIImageView alloc] init];
    self.smallTitleImageV.backgroundColor = [UIColor whiteColor];
    self.smallTitleImageV.image = [UIImage imageNamed:@"flag"];
    [self.contentView addSubview:self.smallTitleImageV];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:22];
    self.titleLabel.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.titleLabel];
    
    self.bigTextLabel = [[UILabel alloc] init];
    //换行时一起用
    self.bigTextLabel.numberOfLines = 0;
    self.bigTextLabel.lineBreakMode = NSLineBreakByCharWrapping;
    self.bigTextLabel.backgroundColor = [UIColor whiteColor];
    
    [self.contentView addSubview:self.bigTextLabel];
    
    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.textAlignment = NSTextAlignmentCenter;
    self.priceLabel.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.priceLabel];
    
    self.likesLabel = [[UILabel alloc] init];
    self.likesLabel.backgroundColor = [UIColor whiteColor];
    self.likesLabel.textAlignment = NSTextAlignmentCenter;

    [self.contentView addSubview:self.likesLabel];
    
    self.heartImageV = [[UIImageView alloc] init];
    self.heartImageV.backgroundColor = [UIColor whiteColor];
    self.heartImageV.image = [UIImage imageNamed:@"heart"];
    [self.likesLabel addSubview:self.heartImageV];
    
    
    self.buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buyButton.backgroundColor = [UIColor cyanColor];
    self.buyButton.alpha = 0.8;
    [self.buyButton setTitle:@"购买" forState:UIControlStateNormal];
    self.buyButton.layer.masksToBounds = YES;
    self.buyButton.layer.cornerRadius = 10;
    [self.buyButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.buyButton];
    
    self.bottomGapView = [[UIView alloc] init];
    self.bottomGapView.backgroundColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:0.8];
    
    [self.contentView addSubview:self.bottomGapView];
    

    
//    UIImageView *ImageV = [[UIImageView alloc] init];
//    ImageV.backgroundColor = [UIColor clearColor];

//    self.image1 = ImageV;
//  //  [self.contentView addSubview:self.image1];
//    
//    
//      UIImageView *ImageV2 = [[UIImageView alloc] init];
//    ImageV2.backgroundColor = [UIColor clearColor];
//
//    self.image2 = ImageV2;
//  //  [self.contentView addSubview:self.image2];
//    
//    UIImageView *ImageV3 = [[UIImageView alloc] init];
//    ImageV3.backgroundColor = [UIColor clearColor];
//
//    self.image3 = ImageV3;
//    [self.contentView addSubview:self.image3];
 

}

-(void)layoutSubviews{


    self.TopGapView.frame = CGRectMake(0, 0, self.frame.size.width, 5);
    self.smallTitleImageV.frame = CGRectMake(5, 25, 22, 22);
    self.titleLabel.frame = CGRectMake(30, 10, self.frame.size.width - 40, 50);
 
    self.bigTextLabel.frame = CGRectMake(10, 60 , self.frame.size.width - 20, 150);
    
    self.priceLabel.frame = CGRectMake(0, self.myImageArray.count * 360 + 300, self.frame.size.width / 3, 50);
    
    self.likesLabel.frame = CGRectMake(self.frame.size.width / 3, self.myImageArray.count * 360 + 300, self.frame.size.width / 3, 50);
    self.heartImageV.frame = CGRectMake(25, 15, 20, 20);

   self.buyButton.frame = CGRectMake(self.frame.size.width / 3 * 2, self.myImageArray.count * 360 + 300, self.frame.size.width / 3, 50);
    self.bottomGapView.frame = CGRectMake(0, self.myImageArray.count * 360 + 350, self.frame.size.width, 5);
    
}

    // descLabel.frame = CGRectMake(10, 250, WIDTH - 20, 200);
    
//    //计算文字高度
//    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
//    //这里的宽度一定要和我们要显示文字的label的宽度一样
//    self.rect = [self.bigTextLabel.text boundingRectWithSize:CGSizeMake(self.frame.size.width - 20, 0) options: NSStringDrawingUsesLineFragmentOrigin  attributes:dic context:nil];
//    self.bigTextLabel.frame = CGRectMake(10, 60, self.frame.size.width - 20, self.rect.size.height);


//      self.image1.frame = CGRectMake(10, 220 + (0 * 360), self.frame.size.width - 20, 350);
//       self.image2.frame = CGRectMake(10, 220 + (1 * 360), self.frame.size.width - 20, 350);
//    self.image3.frame = CGRectMake(10, 220 + (2 * 360), self.frame.size.width - 20, 350);
    
    
    //    UIImage *image = [UIImage imageNamed:self.model.imageStr];
    //
    //    CGFloat heightImage = self.frame.size.width * image.size.height / image.size.width;
    //    self.imageV.frame = CGRectMake(0, 0, self.frame.size.width, heightImage);
    //    //显示图片
    //    self.imageV.image = image;
    
    
    
//    if (self.myImageArray.count == 1) {
//        
//        NewDetailsPicModel *model = self.myImageArray[0];
//        NSString *string1 = [NSString stringWithFormat:@"http://bt.img.17gwx.com/%@",model.picURL];
//        
//        NSData *dataImage1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:string1]];
//        UIImage *image1 = [UIImage imageWithData:dataImage1];
//        CGFloat heightImage = (self.frame.size.width - 20) * image1.size.height / image1.size.width;
//        self.image1.frame = CGRectMake(10, 220, self.frame.size.width - 20, heightImage);
//        
//        // NSString *string = @"http://bt.img.17gwx.com/";         // string = [string stringByAppendingString:model.picURL];
//        [self.image1 sd_setImageWithURL:[NSURL URLWithString:string1]];
//        [self.contentView addSubview:self.image1];
//        //  self.image2.image = nil;
//        //  self.image3.image = nil;
//        
//        
//    }
//    if (self.myImageArray.count == 2) {
//        
//        
//        NewDetailsPicModel *model1 = self.myImageArray[0];
//        NSString *string1 = [NSString stringWithFormat:@"http://bt.img.17gwx.com/%@",model1.picURL];
//        
//        // NSString *string = @"http://bt.img.17gwx.com/";
//        // string = [string stringByAppendingString:model.picURL];
//        [self.image1 sd_setImageWithURL:[NSURL URLWithString:string1]];
//        NSData *dataImage1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:string1]];
//        UIImage *image1 = [UIImage imageWithData:dataImage1];
//        CGFloat heightImage = (self.frame.size.width - 20) * image1.size.height / image1.size.width;
//        self.image1.frame = CGRectMake(10, 220, self.frame.size.width - 20, heightImage);
//        
//        
//        
//        
//        
//        
//        NewDetailsPicModel *model2 = self.myImageArray[1];
//        NSString *string2 = [NSString stringWithFormat:@"http://bt.img.17gwx.com/%@",model2.picURL];
//        
//        // NSString *string = @"http://bt.img.17gwx.com/";
//        // string = [string stringByAppendingString:model.picURL];
//        [self.image2 sd_setImageWithURL:[NSURL URLWithString:string2]];
//        NSData *dataImage2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:string2]];
//        UIImage *image2 = [UIImage imageWithData:dataImage2];
//        CGFloat heightImage2 = (self.frame.size.width - 20) * image1.size.height / image2.size.width;
//        self.image2.frame = CGRectMake(10, self.image1.frame.origin.y + heightImage + 10, self.frame.size.width - 20, heightImage2);
//        
//        
//        [self.contentView addSubview:self.image1];
//        [self.contentView addSubview:self.image2];
//        
//        // self.image3.image = nil;
//    }
//    if (self.myImageArray.count == 3) {
//        
//        
//        NewDetailsPicModel *model1 = self.myImageArray[0];
//        NSString *string1 = [NSString stringWithFormat:@"http://bt.img.17gwx.com/%@",model1.picURL];
//        
//        // NSString *string = @"http://bt.img.17gwx.com/";
//        // string = [string stringByAppendingString:model.picURL];
//        [self.image1 sd_setImageWithURL:[NSURL URLWithString:string1]];
//        
//        NSData *dataImage1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:string1]];
//        UIImage *image1 = [UIImage imageWithData:dataImage1];
//        CGFloat heightImage = (self.frame.size.width - 20) * image1.size.height / image1.size.width;
//        self.image1.frame = CGRectMake(10, 220, self.frame.size.width - 20, heightImage);
//        
//        
//        
//        
//        NewDetailsPicModel *model2 = self.myImageArray[1];
//        NSString *string2 = [NSString stringWithFormat:@"http://bt.img.17gwx.com/%@",model2.picURL];
//        
//        // NSString *string = @"http://bt.img.17gwx.com/";
//        // string = [string stringByAppendingString:model.picURL];
//        [self.image2 sd_setImageWithURL:[NSURL URLWithString:string2]];
//        NSData *dataImage2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:string2]];
//        UIImage *image2 = [UIImage imageWithData:dataImage2];
//        CGFloat heightImage2 = (self.frame.size.width - 20) * image1.size.height / image2.size.width;
//        self.image2.frame = CGRectMake(10, self.image1.frame.origin.y + heightImage + 10, self.frame.size.width - 20, heightImage2);
//        
//        
//        
//        
//        NewDetailsPicModel *model3 = self.myImageArray[2];
//        NSString *string3 = [NSString stringWithFormat:@"http://bt.img.17gwx.com/%@",model3.picURL];
//        
//        // NSString *string = @"http://bt.img.17gwx.com/";
//        // string = [string stringByAppendingString:model.picURL];
//        [self.image3 sd_setImageWithURL:[NSURL URLWithString:string3]];
//        
//        NSData *dataImage3 = [NSData dataWithContentsOfURL:[NSURL URLWithString:string3]];
//        UIImage *image3 = [UIImage imageWithData:dataImage3];
//        CGFloat heightImage3 = (self.frame.size.width - 20) * image1.size.height / image3.size.width;
//        self.image3.frame = CGRectMake(10, self.image1.frame.origin.y + heightImage2 + 10, self.frame.size.width - 20, heightImage3);
//        
//        
//        
//        [self.contentView addSubview:self.image1];
//        [self.contentView addSubview:self.image2];
//        [self.contentView addSubview:self.image3];
    
        //            //for循环加头像
        //            self.myHeadArray = [NSMutableArray array];
        //            self.myHeadArray = [NSMutableArray arrayWithArray:_NDSModel.LikeListArray];
        //            for (int i = 0; i < self.myHeadArray.count; i++) {
        //                UIImageView *ImageV = [[UIImageView alloc] init];
        //                ImageV.layer.masksToBounds = YES;
        //                ImageV.layer.cornerRadius = 15;
        //                ImageV.backgroundColor = [UIColor cyanColor];
        //                ImageV.frame = CGRectMake(10 + (i * 40), 220 + (self.myImageArray.count * 360) + 30, 30, 30);
        //                NewDetailsLikeListModel *model = self.myHeadArray[i];
        //                NSString *string = [NSString stringWithFormat:@"http://7te7t9.com2.z0.glb.qiniucdn.com/%@",model.picURL] ;
        //                [ImageV sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"timg"]];
        //                [self.contentView addSubview:ImageV];
        //                
        //            }
    


-(void)setNDSModel:(NewDetailsSecondModel *)NDSModel{

    _NDSModel = NDSModel;
    
    self.titleLabel.text = _NDSModel.title;
    self.bigTextLabel.text = _NDSModel.desc;
    NSString *string = [NSString stringWithFormat:@"参考价: ¥%@",_NDSModel.price];
    self.priceLabel.text = string;
    self.likesLabel.text = _NDSModel.likes;
   
    //for循环建立大图
    self.myImageArray = [NSMutableArray array];
    self.myImageArray = [NSMutableArray arrayWithArray:_NDSModel.PicArray];
  //  NSLog(@"====sdfsd===%ld",self.myImageArray.count)
  //  NSLog(@"%ld", self.contentView.subviews.count);
//    for (int i = 0; i < self.contentView.subviews.count; i++) {
//        id view = self.contentView.subviews[i];
//        if ([view isKindOfClass:[UIImageView class]]) {
////            NSLog(@"%@", view);
//            [view removeFromSuperview];
//        }
//    }
    
//    int count1 = 0;
//    for (UIView *view in self.contentView.subviews) {
//        if ([view isKindOfClass:[UIImageView class]]) {
//            count1++;
//        }
//    }
//    NSLog(@"++++++%d", count1);
    
    NSLog(@"%ld", self.contentView.subviews.count);
    
    for (int i = 0; i < self.myImageArray.count; i++) {
        UIImageView *ImageV = [[UIImageView alloc] init];
        ImageV.backgroundColor = [UIColor cyanColor];
        ImageV.frame = CGRectMake(10, 220 + (i * 360), self.frame.size.width - 20, 350);

        NewDetailsPicModel *model = self.myImageArray[i];
        
            NSString *string1 = [NSString stringWithFormat:@"http://bt.img.17gwx.com/%@",model.picURL];
//        ImageV.image = [UIImage ima];
            // NSString *string = @"http://bt.img.17gwx.com/";
            // string = [string stringByAppendingString:model.picURL];
            [ImageV sd_setImageWithURL:[NSURL URLWithString:string1] placeholderImage:[UIImage imageNamed:@"timg.jpeg"]];
            
            [self.contentView addSubview:ImageV];
            
        
//        [self.contentView addSubview:ImageV];
        
    }
    
    //for循环加头像
    self.myHeadArray = [NSMutableArray array];
    self.myHeadArray = [NSMutableArray arrayWithArray:_NDSModel.LikeListArray];
    for (int i = 0; i < self.myHeadArray.count; i++) {
        UIImageView *ImageV = [[UIImageView alloc] init];
        ImageV.layer.masksToBounds = YES;
        ImageV.layer.cornerRadius = 15;
        ImageV.backgroundColor = [UIColor cyanColor];
        ImageV.frame = CGRectMake(10 + (i * 40), 220 + (self.myImageArray.count * 360) + 30, 30, 30);
        NewDetailsLikeListModel *model = self.myHeadArray[i];
        NSString *string = [NSString stringWithFormat:@"http://7te7t9.com2.z0.glb.qiniucdn.com/%@",model.picURL] ;
        
        ImageV.image = [UIImage imageNamed:@"timg.jpeg"];
        
        [ImageV sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"timg.jpeg"]];
        [self.contentView addSubview:ImageV];
        
    }
    
//    NSLog(@"%d", self.contentView.subviews.count);
//    int count = 0;
//    for (UIView *view in self.contentView.subviews) {
//        if ([view isKindOfClass:[UIImageView class]]) {
//            count++;
//        }
//    }
//    NSLog(@"--------------%d", count);


}

-(void)buttonAction{

//    NewThirdDetailsViewController *NTDVC = [[NewThirdDetailsViewController alloc] init];
//    NTDVC.url = self.NDSModel.url;

    NSLog(@"1111");
    
    self.myBlock();


}





- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
